#include "ModRunInterpreter.h"
#include <QDebug>

#include "../Output/FactoryOutput.h"

#include "Gof23/Gof_Interpreter/ContextInterpreter.h"
#include "Gof23/Gof_Interpreter/Expression.h"
#include "Gof23/Gof_Interpreter/ExpressionAdd.h"
#include "Gof23/Gof_Interpreter/ExpressionSub.h"
#include "Gof23/Gof_Interpreter/ExpressionEquals.h"

//解释器模式的ModRun（Interpreter）;
ModRunInterpreter::ModRunInterpreter()
{
    //模式名称;
    m_strModelName = "解释器模式（Interpreter）;";
    //模式描述;
    m_strModelDesc = "解释器模式：用于描述如何构成一种简单的语言解释器，主要用于使用面向对象语言开发的编译器和解释器设计\n"
                     "\n"
                     ;
}

//初始化显示模式;
void ModRunInterpreter::InitDisplay()
{
    qDebug() << m_strModelName;
    std::shared_ptr<IOutput> pOutPut = FactoryOutput::GetInstance()->CreateDescOutput();
    if (pOutPut)
    {
        std::initializer_list<QString> outList{ m_strModelName,m_strModelDesc};
        pOutPut->OutPut(outList);
    }
}

//返回模式类型;
EnumModRun ModRunInterpreter::GetEnum()
{
    return emModRunInterpreter;
}

//运行模式;
void ModRunInterpreter::ModRun()
{
    std::shared_ptr<IOutput> pOutPut = FactoryOutput::GetInstance()->CreateRunOutput();
    if (pOutPut)
    {
        std::initializer_list<QString> outListStart{ "========解释器模式（Interpreter） start:=======" };
        std::initializer_list<QString> outListEnd{ "========解释器模式（Interpreter） end:=======" };
        pOutPut->OutPut(outListStart);

        //解释器模式（Interpreter）;
        QString strCommand = "123+456-20";
        //打印表达式;
        std::initializer_list<QString> outListCommand{ "========解释器模式（Interpreter） command:" + strCommand };
        pOutPut->OutPut(outListCommand);

        ContextInterpreter Context(strCommand);

        ExpressionAll expAll;
        ExpressionAdd expAdd(&expAll);
        ExpressionSub expSub(&expAll);
        ExpressionEquals expEquals(&expAll);

        uint64_t uValue = expAll.Interpreter(Context);

        //打印计算结果;
        std::initializer_list<QString> outListResult{ "========解释器模式（Interpreter） result:" +  QString::number(uValue) };
        pOutPut->OutPut(outListResult);

        pOutPut->OutPut(outListEnd);
    }

}

InterFaceModRun* ModRunInterpreter::CreateMe()
{
    return new ModRunInterpreter();
}
